package com.danny.service;

import com.spring.BeanPostProcessor;
import com.spring.BeansException;
import com.spring.Commonent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Commonent
public class DyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization: " + beanName);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization: " + beanName);
//        if (beanName.equals("userService")) {
//            Object proxyInstance = Proxy.newProxyInstance(DyBeanPostProcessor.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
//                @Override
//                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//                    System.out.println("切面逻辑");
//                    return method.invoke(bean, args);
//                }
//            });
//            return proxyInstance;
//        }
        return bean;
    }
}
